<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */

namespace MoChat\App\Call\Action\Dashboard;

use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Rbac\Middleware\PermissionMiddleware;
use MoChat\App\Call\Logic\MonitorIndexLogic;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;

/**
 * 示闲示忙.
 * @Controller
 */
class CallListOne extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/call/callListset", methods="POST")
     */
    public function handle(): array
    {
        $user = user();
        if (empty($user)) {
            throw new CommonException(ErrorCode::AUTH_UNAUTHORIZED, '登入异常请重新登入');
        }
        // 请求参数
        $data = $this->request->all();

        $token = send_http_request(env("CALL_CENTER_BASE_URL"), '/api/login', 'GET', [
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'verify' => false,
            'query' => [
                'username' => env("CALL_CENTER_USERNAME"),
                'password' => env("CALL_CENTER_PASSWORD"),
            ]
        ]);
        $response = send_http_request(env("CALL_CENTER_BASE_URL"), '/api/ola/agent/'.$data['type'].'?agent='.$data['agent'], 'POST', [
            'headers' => [
                'X-Auth-Token' => $token,
                'Content-Type' => 'application/json',
            ],
            'verify' => false,
        ]);
        $response = my_array_change_key($response);
        return [];
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
            'type' => 'in:1,2,3,4,5',
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
            'type.in' => 'type参数值错误',
        ];
    }
}
